#! -*- coding utf-8 -*-
#! @Time   :$[DATE] $[TIME]
#! @Author :gg

import re

def comupute_mut_and_div(formula):
    '''算乘除'''
    operators = re.findall("[*/]", formula)7+
    calc_list = re.split("[*/]", formula)
    res = None    # 细节赋值，第一遍for循环轮空
    for index, i in enumerate(calc_list):
        if res:    # 第一次这里不执行，所以列表索引不会超出范围
            if operators[index-1] == "*":
                res *= float(i)
            elif operators[index-1] == "/":
                res /= float(i)
        else:
            res = float(i)
    print("\033[31;1m[%s]运算结果=\033[0m" % formula, res)
    return res

def handle_special_sign(plus_and_sub_operators, mul_and_div):
    """
    有时会遇到这种情况：1-2*-2/1
    plus_and_sub_operators：['-', '-']
    mul_and_div：['1', '2*', '2/1']
    :param plus_and_sub_operators:
    :param mul_and_div:
    :return:
    """
    for index, i in enumerate(mul_and_div):
        i = i.strip()
        if i.endswith("*") or i.endswith("/"):
            mul_and_div[index] = mul_and_div[index] + plus_and_sub_operators[index] + mul_and_div[index+1]
            del mul_and_div[index+1]
            del plus_and_sub_operators[index]
    return plus_and_sub_operators, mul_and_div

def handle_minus_inlist(operators_list, calc_list):
    """
    处理加减带负号的运算，1--4 = 1-(-4) = 1+4
    operators_list: ['-']
    calc_list: ['1', '', '4.0']
    :param operators_list:
    :param calc_list:
    :return:
    """
    for index, i in enumerate(calc_list):
        if i == '':
            calc_list[index+1] = "-" + calc_list[index+1]
            del calc_list[index]
    return operators_list, calc_list

def compute(formula):
    '''
    解析字符串
    :param formula: 输入的为字符串
    :return: 计算结果
    '''
    formula = formula.strip("()")  # 去括号,这里是（1+2-2*3+3/6）
    plus_and_sub_operators = re.findall("[+-]", formula)
    mul_and_div = re.split("[+-]", formula)  # 取出乘除公式

    plus_and_sub_operators, mul_and_div = handle_special_sign(plus_and_sub_operators, mul_and_div)
    for i in mul_and_div:
        if len(i) > 1:
            print(i)
            res = comupute_mut_and_div(i)
            formula = formula.replace(i, str(res))
        else:
            continue
    calc_list = re.split("[+-]", formula)  # 去除乘除公式
    plus_and_sub_operators, calc_list = handle_minus_inlist(plus_and_sub_operators, calc_list)
    total_res = None
    for index, i in enumerate(calc_list):
        if total_res:
            if plus_and_sub_operators[index - 1] == "-":
                total_res -= float(i)
            elif plus_and_sub_operators[index - 1] == "+":
                total_res += float(i)
        else:
            total_res = float(i)
    print("\033[32;1m[%s]运算结果:\033[0m" % formula, total_res)
    return total_res

def calc(formula):
    '''去括号'''
    brackets_flag = True
    while brackets_flag:
        m = re.search("\([^()]*\)", formula)
        if m:
            sub_res = compute(m.group())    # 替换
            formula = formula.replace(m.group(), str(sub_res))
        else:
            print("最终结果：", compute(formula))
            brackets_flag = False

if __name__ == '__main__':
    # formula = "-1-2*((60-30+(-40/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))"
    formula = input("请输入：")
    calc(formula)